package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.entity.Link;
import com.itheima.ydd.service.LinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author kong
 * @date:2022/4/10
 * 友情链接管理
 */
@RestController
@RequestMapping("/ydd_link")
@Slf4j
public class LinkController {
    @Autowired
     private LinkService linkService;

    /**
     * 链接详细信息
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public Result rowInfo(Integer id){

        return Result.ok(linkService.getById(id));
    }

    /**
     * 修改链接
     * @param link
     * @return
     */
  @PostMapping("/update")
    public Result update(@RequestBody Link link){
        linkService.updateById(link);
        return Result.ok("修改成功");
  }

    /**
     * 删除链接
     * @param id
     * @return
     */

  @GetMapping("/del")
    public Result del(Integer id){
        linkService.removeById(id);
        return Result.ok("删除成功");
  }

    /**
     * 添加链接
     * @param link
     * @return
     */
  @PostMapping("/add")
    public Result add(@RequestBody Link link){
      linkService.save(link);
      return Result.ok("添加成功");
  }

    /**
     * 链接列表
     * @param type
     * @param page
     * @param limit
     * @return
     */

  @GetMapping("/list")
    public Result list(Integer type,Integer page,Integer limit){
      LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(type != null,Link::getType,type);
      Page<Link> pageInfo = new Page<>(page,limit);
      linkService.page(pageInfo,wrapper);
      return Result.ok(PageResult.init(pageInfo));

  }
}
